home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
TEMP
/
GNU
/
flex
/
Generateds
< prev
next >
Wrap
Text File
|
1995-06-28
|
5KB
|
119 lines
Generated scanner
Previous: <Actions=>Actions> * Next: <Start conditions=>Startcondi> * Up: <Top=>!Root>
#Wrap on
{fH3}The generated scanner{f}
The output of {fCode}flex{f} is the file {fCite}lex.yy.c{f}, which contains
the scanning routine {fEmphasis}yylex(){f}, a number of tables used by
it for matching tokens, and a number of auxiliary routines
and macros. By default, {fEmphasis}yylex(){f} is declared as follows:
#Wrap off
#fCode
int yylex()
\{
… various definitions and the actions in here …
\}
#f
#Wrap on
(If your environment supports function prototypes, then it
will be "int yylex( void )".) This definition may be
changed by defining the "YY\_DECL" macro. For example, you
could use:
#Wrap off
#fCode
\#define YY\_DECL float lexscan( a, b ) float a, b;
#f
#Wrap on
to give the scanning routine the name {fCode}lexscan{f}, returning a
float, and taking two floats as arguments. Note that if
you give arguments to the scanning routine using a
K&R-style\/non-prototyped function declaration, you must
terminate the definition with a semi-colon ({fEmphasis};{f}).
Whenever {fEmphasis}yylex(){f} is called, it scans tokens from the
global input file {fCode}yyin{f} (which defaults to stdin). It
continues until it either reaches an end-of-file (at which
point it returns the value 0) or one of its actions
executes a {fCode}return{f} statement.
If the scanner reaches an end-of-file, subsequent calls are undefined
unless either {fCode}yyin{f} is pointed at a new input file (in which case
scanning continues from that file), or {fEmphasis}yyrestart(){f} is called.
{fEmphasis}yyrestart(){f} takes one argument, a {fEmphasis}FILE \*{f} pointer (which
can be nil, if you've set up {fCode}YY\_INPUT{f} to scan from a source
other than {fCode}yyin{f}), and initializes {fCode}yyin{f} for scanning from
that file. Essentially there is no difference between just assigning
{fCode}yyin{f} to a new input file or using {fEmphasis}yyrestart(){f} to do so;
the latter is available for compatibility with previous versions of
{fCode}flex{f}, and because it can be used to switch input files in the
middle of scanning. It can also be used to throw away the current
input buffer, by calling it with an argument of {fCode}yyin{f}; but
better is to use {fCode}YY\_FLUSH\_BUFFER{f} (see above). Note that
{fEmphasis}yyrestart(){f} does {fEmphasis}not{f} reset the start condition to
{fCode}INITIAL{f} (see Start Conditions, below).
If {fEmphasis}yylex(){f} stops scanning due to executing a {fCode}return{f}
statement in one of the actions, the scanner may then be called
again and it will resume scanning where it left off.
By default (and for purposes of efficiency), the scanner
uses block-reads rather than simple {fEmphasis}getc(){f} calls to read
characters from {fCode}yyin{f}. The nature of how it gets its input
can be controlled by defining the {fCode}YY\_INPUT{f} macro.
YY\_INPUT's calling sequence is
"YY\_INPUT(buf,result,max\_size)". Its action is to place
up to {fStrong}max\_size{f} characters in the character array {fStrong}buf{f} and
return in the integer variable {fStrong}result{f} either the number of
characters read or the constant YY\_NULL (0 on Unix
systems) to indicate EOF. The default YY\_INPUT reads from
the global file-pointer "yyin".
A sample definition of YY\_INPUT (in the definitions
section of the input file):
#Wrap off
#fCode
%\{
\#define YY\_INPUT(buf,result,max\_size) \\
\{ \\
int c = getchar(); \\
result = (c == EOF) ? YY\_NULL : (buf[0] = c, 1); \\
\}
%\}
#f
#Wrap on
This definition will change the input processing to occur
one character at a time.
When the scanner receives an end-of-file indication from
YY\_INPUT, it then checks the {fEmphasis}yywrap(){f} function. If
{fEmphasis}yywrap(){f} returns false (zero), then it is assumed that the
function has gone ahead and set up {fCode}yyin{f} to point to
another input file, and scanning continues. If it returns
true (non-zero), then the scanner terminates, returning 0
to its caller. Note that in either case, the start
condition remains unchanged; it does {fEmphasis}not{f} revert to {fCode}INITIAL{f}.
If you do not supply your own version of {fEmphasis}yywrap(){f}, then you
must either use {fEmphasis}%option noyywrap{f} (in which case the scanner
behaves as though {fEmphasis}yywrap(){f} returned 1), or you must link with
{fEmphasis}-lfl{f} to obtain the default version of the routine, which always
returns 1.
Three routines are available for scanning from in-memory
buffers rather than files: {fEmphasis}yy\_scan\_string(){f},
{fEmphasis}yy\_scan\_bytes(){f}, and {fEmphasis}yy\_scan\_buffer(){f}. See the discussion
of them below in the section Multiple Input Buffers.
The scanner writes its {fEmphasis}ECHO{f} output to the {fCode}yyout{f} global
(default, stdout), which may be redefined by the user
simply by assigning it to some other {fCode}FILE{f} pointer.